var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listCount").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight);
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*window.fcolMinWidth;
	}
	$("#listCount").datagrid({
					url:'/backbycount/getcount',
					title:'已提交统计结果列表',
					method:'get',
					pagination:true,
					pageList: [10,20,50,200],
					loadMsg:'加载中......',
					nowrap:true,
					columns:[[ 
					        {field:'ck',checkbox:true},
					        {field:'rowid' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{param}","g");
								 var ref=opt.replace(re,row.id);
								 return ref;
							 }},
					        {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
					        {field:'shopname',title:'门店名称', align:'left',width:100},
							{field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
							{field:'dealername',title:'经销商名称', align:'left',width:100},
							{field:'counttotal',title:'统计金额', align:'left',width:100},
							{field:'deposit',title:'押金', align:'left',width:100},
							{field:'returned',title:'已返款', align:'left',width:100},
							{field:'stand',title:'合格数量', align:'left',width:100,formatter:function(value,row,index){
								if(value==''||value==undefined){
									return 0;
								}
								return value;
							}},
							{field:'unstand',title:'不合格数量', align:'left',width:100,formatter:function(value,row,index){
								if(value==''||value==undefined){
									return 0;
								}
								return value;
							}},
							{field:'cmonth',title:'统计月份', align:'left',width:80},
							{field:'counttime',title:'统计时间', align:'left',width:130,sortable:true,formatter:function(value,row,index){
								 var datetime=Common.formatterDate2(value);
								 return datetime;
							}},
							{field:'countname',title:'统计人', align:'left',width:100},
					]]
				});
	$('#countbeginTime').datetimebox({});  
	$('#countendTime').datetimebox({});
	$("#serbutton").click(function(){
		vo.isback=$("#isflag").val();
		vo.dealerid=$("#dealerid").val();
		vo.dealername=$("#dealername").val();
		vo.shopid=$("#shopid").val();
		vo.shopname=$("#shopname").val();
		vo.backbegintime=$('#backbeginTime').datetimebox('getValue');
		vo.backendtime=$('#backendTime').datetimebox('getValue');
		vo.countbegintime=$('#countbeginTime').datetimebox('getValue');
		vo.countendtime=$('#countendTime').datetimebox('getValue');
		$("#listCount").datagrid('load',vo);
	});
	if(opt==''){
		$("#listCount").datagrid('hideColumn','rowid');
	}
	$("#listCount").datagrid({
		toolbar: '#tb'
	});
});
/**
 * 驳回
 * @param id
 */
function reject(id){
	if(id==''){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.prompt('驳回','请输入驳回原因？',function(data){
		if(data==''){
			$.messager.alert("失败","驳回原因不可为空",'error');
			return;
		}
		if(data){
			$.ajax({
				type:'post',
				url: '/backbycount/reject',
				data:{id:id,rejreason:data},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","驳回成功",'info',function(){
							$("#listCount").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","驳回失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","驳回失败：",'error');
				}
			});
		}
	});
}
/**
 * 批量驳回
 */
function betchReject(){
	var rows=$("#listCount").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.prompt('驳回','请输入驳回原因？',function(data){
		if(data==''){
			$.messager.alert("失败","驳回原因不可为空",'error');
			return;
		}
		if(data){
			var ids='';
			var isback=true;
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].id+',';
			}
			$.ajax({
				type:'post',
				url: '/backbycount/betchReject',
				data:{ids:ids,rejreason:data},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","驳回成功",'info',function(){
							$("#listCount").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","驳回失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","驳回失败：",'error');
				}
			});
		}
	});
}
/**
 * 详情
 */
function viewCount(id){
	window.parent.openDialog("统计详情",'/backbycount/viewCount','',400,500,'id='+id);
}